package com.supply.dl.app.http;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.supply.dl.R;
import com.supply.dl.SupplyApplication;
import com.supply.dl.utils.Utils;

/**
 * Description: 加载框
 * Author：skyTian
 * Time:2017/11/13 10:51
 * CopyRight:com,sky
 */
public class LoadingDialog extends DialogFragment {

    /**
     * 根布局
     */
    private View rootView;

    private TextView show_msg_tv;

    public static LoadingDialog newInstance(Bundle bundle) {
        LoadingDialog loadingDialog = new LoadingDialog();

        if (bundle != null)
            loadingDialog.setArguments(bundle);

        return loadingDialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (getDialog() != null) {
            getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
            getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }

        rootView = inflater.inflate(R.layout.progress_layout, container, false);
        initView();
        return rootView;
    }


    private void initView() {
        show_msg_tv = rootView.findViewById(R.id.show_msg_tv);

        if (getArguments() != null && getArguments().containsKey("showMsg")) {
            show_msg_tv.setText(getArguments().getString("showMsg"));
        }
    }


    public void setShowTxt(String showMsg) {
        show_msg_tv.setText(showMsg);
    }

    public void show(Context context) {
        show(((Activity) context).getFragmentManager(), "");
    }

    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow();
        WindowManager.LayoutParams winParams = window.getAttributes();
        winParams.dimAmount = 0.0f;

        window.setAttributes(winParams);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getDialog() != null) {
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#80FFFFFF")));
            if (getActivity() != null)
                getDialog().getWindow().setLayout((Utils.getWindowWidth(SupplyApplication.getAppContext())), (Utils.getWindowHeigh(SupplyApplication.getAppContext())));
        }
    }

}
